package com.ilop.sthome.utils;

import android.content.Context;

import com.ilop.sthome.data.enums.SmartDevice;
import com.siterwell.familywellplus.R;

/**
 * @author skygge
 * @date 2020-01-01.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：解析设备状态
 */
public class DeviceStatusUtil {

    /**
     * 网关的报警（与子设备无关，这些报警由网关判断）
     * @param context 上下文
     * @param deviceStatus 网关状态
     * @return 网关状态
     */
    public static String getGatewayAlert(Context context,String deviceStatus){

        if("00000000".equals(deviceStatus)){
            return context.getResources().getString(R.string.electric_city_break_off);
        }else if("00000001".equals(deviceStatus)){
            return context.getResources().getString(R.string.electric_city_normal);
        }else if("00000002".equals(deviceStatus)){
            return context.getResources().getString(R.string.battery_normal);
        }else if("00000003".equals(deviceStatus)){
            return context.getResources().getString(R.string.battery_low);
        }else if("00000004".equals(deviceStatus)){
            return context.getResources().getString(R.string.ali_gateway_restart);
        }else if("00000005".equals(deviceStatus)){
            return context.getResources().getString(R.string.ali_net_break);
        }else if("00000006".equals(deviceStatus)){
            return context.getResources().getString(R.string.ali_net_ok);
        }else if("00000007".equals(deviceStatus)){
            return context.getResources().getString(R.string.gateway_alert);
        }else if("00000008".equals(deviceStatus)){
            return context.getResources().getString(R.string.ali_gateway_init);
        }else if("00000009".equals(deviceStatus)){
            return context.getResources().getString(R.string.ali_gateway_config);
        }else if("0000000A".equals(deviceStatus)){
            return context.getResources().getString(R.string.ali_gateway_stop_alert);
        }else {
            return context.getResources().getString(R.string.unknown_error);
        }
    }

    /**
     * 获取子设备报警信息
     * @param context 上下文
     * @param deviceType 设备类型
     * @param deviceStatus 设备状态
     * @return status
     */
    public static String getAlert(Context context, String deviceType, String deviceStatus){
        try {
            String alert_status = deviceStatus.substring(4,6);
            String battery = deviceStatus.substring(2,4);
            SmartDevice mDevice = SmartDevice.getType(deviceType);
            switch (mDevice) {
                case EE_DEV_DOOR:
                    switch (alert_status) {
                        case "55":
                            return context.getString(R.string.door_open);
                        case "66":
                            return context.getString(R.string.door_still_open);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.door_close);
                            }
                        case "A0":
                            return context.getString(R.string.has_been_moved);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_SOS:
                    switch (alert_status) {
                        case "55":
                            return context.getString(R.string.help);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        case "66":
                            return context.getString(R.string.offline);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_PIR:
                    switch (alert_status) {
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        case "A0":
                            return context.getString(R.string.has_been_moved);
                        case "51":
                        case "52":
                        case "55":
                            return context.getString(R.string.has_people);
                        case "11":
                            return context.getString(R.string.Illegal_demolition);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_SM_ALARM:
                    switch (alert_status) {
                        case "11":
                            return context.getString(R.string.Illegal_demolition);
                        case "BB":
                            return context.getString(R.string.test);
                        case "55":
                            return context.getString(R.string.alarm);
                        case "50":
                            return context.getString(R.string.silence);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_CO_ALARM:
                case EE_DEV_WT_ALARM:
                case EE_DEV_GAS_ALARM:
                case EE_DEV_THERM_ALARM:
                    switch (alert_status) {
                        case "11":
                            return context.getString(R.string.Illegal_demolition);
                        case "50":
                            return context.getString(R.string.silence);
                        case "55":
                            return context.getString(R.string.alarm);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        case "BB":
                            return context.getString(R.string.test);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_TH_CHECK:
                    if ("FF".equals(alert_status)) {
                        return context.getString(R.string.offline);
                    } else {
                        String realT;
                        String temp = deviceStatus.substring(4, 6);
                        String humidity = deviceStatus.substring(6, 8);
                        String temp2 = Integer.toBinaryString(Integer.parseInt(temp, 16));
                        if (temp2.length() == 8) {
                            realT = "-" + (128 - Integer.parseInt(temp2.substring(1), 2));
                        } else {
                            realT = "" + Integer.parseInt(temp2, 2);
                        }
                        if (Integer.parseInt(realT) > 100 || Integer.parseInt(realT) < -40 || Integer.parseInt(humidity, 16) < 0 || Integer.parseInt(humidity, 16) > 100) {
                            return context.getString(R.string.offline);
                        } else {
                            return context.getString(R.string.normal);
                        }
                    }
                case EE_DEV_SX_SM_ALARM:
                    switch (alert_status) {
                        case "17":
                            return context.getString(R.string.cxalert7);
                        case "19":
                            return context.getString(R.string.cxalert9);
                        case "12":
                            return context.getString(R.string.cxalert2);
                        case "15":
                            return context.getString(R.string.cxalert5);
                        case "1B":
                            return context.getString(R.string.cxalert11);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_NEW_GAS_ALARM:
                    switch (alert_status) {
                        case "1C":
                            return context.getString(R.string.has_been_moved);
                        case "11":
                            return context.getString(R.string.Illegal_demolition);
                        case "50":
                            return context.getString(R.string.silence);
                        case "55":
                            return context.getString(R.string.alarm);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        case "BB":
                            return context.getString(R.string.test);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_CO_GAS_ALARM:
                    switch (alert_status) {
                        case "11":
                            return context.getString(R.string.co_sensor_fault);
                        case "12":
                            return context.getString(R.string.gas_sensor_fault);
                        case "17":
                            return context.getString(R.string.test);
                        case "18":
                            return context.getString(R.string.co_alarm);
                        case "19":
                            return context.getString(R.string.gas_alarm);
                        case "1A":
                            return context.getString(R.string.co_alarm_silence);
                        case "1B":
                            return context.getString(R.string.gas_alarm_silence);
                        case "1C":
                            return context.getString(R.string.has_been_moved);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_MODE_BUTTON:
                    switch (alert_status) {
                        case "01":
                        case "02":
                        case "04":
                        case "08":
                            return context.getString(R.string.normal);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        case "55":
                            return context.getString(R.string.help);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_LOCK:
                    switch (alert_status) {
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        case "AB":
                            return context.getString(R.string.actived);
                        case "10":
                            return context.getString(R.string.illegal_operation);
                        case "20":
                            return context.getString(R.string.dismantle);
                        case "30":
                            return context.getString(R.string.coercion);
                        case "40":
                            return context.getString(R.string.low_battery);
                        case "51":
                            return context.getString(R.string.password_open);
                        case "52":
                            return context.getString(R.string.card_open);
                        case "53":
                            return context.getString(R.string.footprint_open);
                        case "55":
                            return context.getString(R.string.remote_unlock_success);
                        case "56":
                            return context.getString(R.string.password_error);
                        case "60":
                            return context.getString(R.string.alarm_remove);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_BUTTON:
                    switch (alert_status) {
                        case "01":
                            return context.getString(R.string.trigger);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_SOCKET:
                    String alert_status1 = deviceStatus.substring(6, 8);
                    if ("01".equals(alert_status1)) {
                        return context.getResources().getString(R.string.open);
                    } else if ("00".equals(alert_status1)) {
                        return context.getString(R.string.close);
                    } else {
                        return context.getString(R.string.offline);
                    }
                case EE_DEV_LIGHTING_MODULE:
                    if ("01".equals(alert_status)) {
                        return context.getResources().getString(R.string.open);
                    } else if ("00".equals(alert_status)) {
                        return context.getString(R.string.close);
                    } else {
                        return context.getString(R.string.offline);
                    }
                case EE_TEMP_OUTDOOR_SIREN:
                    switch (alert_status) {
                        case "A0":
                            return context.getString(R.string.has_been_moved);
                        case "A1":
                            return context.getString(R.string.low_battery);
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_TEMP_CONTROL:
                    switch (alert_status) {
                        case "FF":
                        case "FE":
                            return context.getString(R.string.offline);
                        default:
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                    }
                case EE_DEV_FLASH_ALARM:
                    switch (alert_status) {
                        case "11":
                            return context.getString(R.string.co_alarm);
                        case "22":
                            return context.getString(R.string.smalarm);
                        case "33":
                            return context.getString(R.string.illegal_encroachment);
                        case "44":
                            return context.getString(R.string.warn_alarm);
                        case "55":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return context.getString(R.string.low_battery);
                            } else {
                                return context.getString(R.string.normal);
                            }
                        case "AA":
                            return context.getString(R.string.test);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_MANIPULATOR:
                    switch (alert_status) {
                        case "01":
                        case "03":
                            return context.getString(R.string.open);
                        case "02":
                        case "04":
                            return context.getString(R.string.close);
                        default:
                            return context.getString(R.string.offline);
                    }
                case EE_DEV_SOLENOID_VALVE:
                    switch (alert_status) {
                        case "01":
                            return context.getString(R.string.open);
                        case "02":
                            return context.getString(R.string.close);
                        default:
                            return context.getString(R.string.offline);
                    }
                default:
                    return context.getString(R.string.offline);
            }
        }catch (Exception e){
            return context.getString(R.string.offline);
        }
    }

    /**
     * 获取设备状态颜色
     * @param deviceType 设备类型
     * @param deviceStatus 设备状态码
     * @return 设备状态
     */
    public static int getStatus(String deviceType, String deviceStatus){
        try {
            String alert_status = deviceStatus.substring(4,6);
            String battery = deviceStatus.substring(2,4);
            SmartDevice mDevice = SmartDevice.getType(deviceType);
            switch (mDevice) {
                case EE_DEV_DOOR:
                    switch (alert_status) {
                        case "A0":
                            return 0;
                        case "55":
                        case "66":
                            return 1;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        default:
                            return 3;
                    }
                case EE_DEV_SOS:
                    switch (alert_status) {
                        case "55":
                            return 0;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        case "66":
                            return 3;
                        default:
                            return 3;
                    }
                case EE_DEV_PIR:
                    switch (alert_status) {
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        case "A0":
                            return 0;
                        case "51":
                        case "52":
                        case "55":
                            return 1;
                        case "11":
                            return 2;
                        default:
                            return 3;
                    }
                case EE_DEV_SM_ALARM:
                    switch (alert_status) {
                        case "11":
                            return 2;
                        case "BB":
                        case "55":
                            return 0;
                        case "50":
                            return 4;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        default:
                            return 3;
                    }
                case EE_DEV_CO_ALARM:
                case EE_DEV_WT_ALARM:
                case EE_DEV_GAS_ALARM:
                case EE_DEV_THERM_ALARM:
                    switch (alert_status) {
                        case "11":
                            return 2;
                        case "50":
                            return 4;
                        case "55":
                        case "BB":
                            return 0;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        default:
                            return 3;
                    }
                case EE_DEV_TH_CHECK:
                    if ("FF".equals(alert_status)) {
                        return 3;
                    } else {
                        String realT;
                        String temp = deviceStatus.substring(4, 6);
                        String humidity = deviceStatus.substring(6, 8);
                        String temp2 = Integer.toBinaryString(Integer.parseInt(temp, 16));
                        if (temp2.length() == 8) {
                            realT = "-" + (128 - Integer.parseInt(temp2.substring(1), 2));
                        } else {
                            realT = "" + Integer.parseInt(temp2, 2);
                        }
                        if (Integer.parseInt(realT) > 100 || Integer.parseInt(realT) < -40 || Integer.parseInt(humidity, 16) < 0 || Integer.parseInt(humidity, 16) > 100) {
                            return 3;
                        } else {
                            return 4;
                        }
                    }
                case EE_DEV_SX_SM_ALARM:
                    switch (alert_status) {
                        case "12":
                        case "15":
                            return 2;
                        case "17":
                        case "19":
                            return 0;
                        case "1B":
                            return 1;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        default:
                            return 3;
                    }
                case EE_DEV_NEW_GAS_ALARM:
                    switch (alert_status) {
                        case "11":
                            return 2;
                        case "50":
                            return 4;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        case "1C":
                        case "55":
                        case "BB":
                            return 0;
                        default:
                            return 3;
                    }
                case EE_DEV_CO_GAS_ALARM:
                    switch (alert_status) {
                        case "11":
                        case "12":
                            return 2;
                        case "17":
                        case "18":
                        case "19":
                            return 0;
                        case "1A":
                        case "1B":
                            return 4;
                        case "1C":
                            return 1;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        default:
                            return 3;
                    }
                case EE_DEV_MODE_BUTTON:
                    switch (alert_status) {
                        case "01":
                        case "02":
                        case "04":
                        case "08":
                            return 4;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        case "55":
                            return 0;
                        default:
                            return 3;
                    }
                case EE_DEV_LOCK:
                    switch (alert_status) {
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        case "AB":
                        case "60":
                            return 4;
                        case "10":
                        case "20":
                            return 0;
                        case "30":
                        case "51":
                        case "52":
                        case "53":
                        case "55":
                        case "56":
                            return 1;
                        case "40":
                            return 2;
                        default:
                            return 3;
                    }
                case EE_DEV_BUTTON:
                    switch (alert_status) {
                        case "01":
                            return 1;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        default:
                            return 3;
                    }
                case EE_DEV_SOCKET:
                    String alert_status1 = deviceStatus.substring(6, 8);
                    if ("01".equals(alert_status1)) {
                        return 1;
                    } else if ("00".equals(alert_status1)) {
                        return 4;
                    } else {
                        return 3;
                    }
                case EE_DEV_LIGHTING_MODULE:
                    if ("01".equals(alert_status)) {
                        return 1;
                    } else if ("00".equals(alert_status)) {
                        return 4;
                    } else {
                        return 3;
                    }
                case EE_TEMP_OUTDOOR_SIREN:
                    switch (alert_status) {
                        case "A0":
                            return 0;
                        case "A1":
                            return 2;
                        case "AA":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        default:
                            return 3;
                    }
                case EE_TEMP_CONTROL:
                    switch (alert_status) {
                        case "FF":
                        case "FE":
                            return 3;
                        default:
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                    }
                case EE_DEV_FLASH_ALARM:
                    switch (alert_status) {
                        case "11":
                        case "22":
                        case "33":
                        case "44":
                        case "55":
                            if (Integer.parseInt(battery, 16) <= 15) {
                                return 2;
                            } else {
                                return 4;
                            }
                        case "AA":
                            return 0;
                        default:
                            return 3;
                    }
                case EE_DEV_MANIPULATOR:
                    switch (alert_status) {
                        case "01":
                        case "03":
                            return 1;
                        case "02":
                        case "04":
                            return 4;
                        default:
                            return 3;
                    }
                case EE_DEV_SOLENOID_VALVE:
                    switch (alert_status) {
                        case "01":
                            return 1;
                        case "02":
                            return 4;
                        default:
                            return 3;
                    }
                default:
                    return 3;
            }
        }catch (Exception e){
            return 3;
        }
    }

}
